package cn.com.idmy.cloud.util;

import com.alibaba.fastjson2.JSONObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

public class RestUtil {
    private static final RestTemplate restTemplate = new RestTemplate();

    @Nullable
    public static JSONObject get(@NotNull String url) {
        var response = restTemplate.getForEntity(URI.create(url), JSONObject.class);
        return response.getBody();
    }

    @Nullable
    public static JSONObject post(@NotNull String url, @NotNull JSONObject json) {
        var headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        var params = new HttpEntity<>(json, headers);
        var response = restTemplate.postForEntity(URI.create(url), params, JSONObject.class);
        return response.getBody();
    }
}
